$(function() {
    var mainCtrl = avalon.define({
        $id: "mainController",
        linkUrl: "",
        picUrl: "",
        beginDate: "",
        endDate: "",
        pageId: "",
        savePage: function() {
            if (mainCtrl.picUrl == "") {
            	PLATFORM.showTips("warning", "图片不能为空");
                return;
            }
            if (mainCtrl.linkUrl == "") {
            	PLATFORM.showTips("warning", "链接不能为空");
                return;
            }
            if (mainCtrl.beginDate == "" || mainCtrl.endDate == "") {
            	PLATFORM.showTips("warning", "开始,结束时间不能为空");
                return;
            }
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "id": mainCtrl.pageId,
                    "picUrl": mainCtrl.picUrl,
                    "linkUrl": mainCtrl.linkUrl,
                    "endDate": mainCtrl.endDate,
                    "beginDate": mainCtrl.beginDate
                },
                url: DI.saveMssAggregationPage,
                success: function(data) {
                    if (data.retFlag == 1) {
                    	PLATFORM.showTips("success", "保存成功");
                    	// 重新获取数据,ID
                    	mainCtrl.queryPage();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        queryPage: function() {
        	// 初始化查询记录
            $.ajax({
                type: "post",
                dataType: "json",
                url: DI.queryMssAggregationPage,
                success: function(data) {
                    if (data.retFlag == 1 && (typeof data.data) != "undefined") {
                    	mainCtrl.pageId = data.data.id;
                        mainCtrl.picUrl = data.data.picUrl;
                        mainCtrl.linkUrl = data.data.linkUrl ;
                        mainCtrl.endDate = data.data.endDate;
                        mainCtrl.beginDate = data.data.beginDate;
                        $("#endDate").datepicker("option", "minDate", mainCtrl.beginDate);
                        $("#beginDate").datepicker("option", "maxDate", mainCtrl.endDate);
                    } else {
                    	// 保存之后再查询，可能返回是空数据,要将老数据置空
                    	mainCtrl.pageId = "";
                        mainCtrl.picUrl = "";
                        mainCtrl.linkUrl = "" ;
                        mainCtrl.endDate = "";
                        mainCtrl.beginDate = "";
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        changePic: function() {
        	$("#upladFile").click();
        }
    });
    var popCtrl = avalon.define({
        $id: "popController",
        uploadFile: function() {
            if ($("#upladFile").val() == "") {
                return;
            }
            $("#form_file").ajaxSubmit({
                type: "post",
                dataType: "json",
                url: DI.uploadMssPic,
                success: function(data) {
                    if (data.retFlag == 1) {
                    	mainCtrl.picUrl = cmfHeadHost + data.data;
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                    $("#upladFile").after($("#upladFile").clone().val(""));
                    $("#upladFile").remove();
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                	PLATFORM.showTips("error", "服务异常,请稍后再试");
                    console.log("ajax error");
                    console.log(XMLHttpRequest);
                }
            });
        }
    });

    //PLATFORM.timepickerRange("#beginDate", "#endDate");
    $("#beginDate").datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate) {
            $("#endDate").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#endDate").datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate) {
            $("#beginDate").datepicker("option", "maxDate", selectedDate);
        }
    });
    // 初始化记录
    mainCtrl.queryPage();
    $("#form_file").on("change", "#upladFile", function(){
    	popCtrl.uploadFile();
    });
    avalon.scan();
    
});
